<template>
  <div class="auth-container">
    <div class="auth-header">
      <div class="logo-container">
        <span class="system-name">农产品宣传直播销售系统</span>
      </div>
    </div>

    <div class="auth-box">
      <el-card class="auth-card">
        <h2 class="auth-title">用户登录</h2>
        <el-form :model="user" class="auth-form" label-position="top">
          <el-form-item label="用户名">
            <el-input
                v-model="user.username"
                placeholder="请输入用户名"
                clearable
            />
          </el-form-item>

          <el-form-item label="密码">
            <el-input
                v-model="user.password"
                type="password"
                placeholder="请输入密码"
                show-password
            />
          </el-form-item>

          <el-form-item>
            <el-button
                type="primary"
                class="auth-button"
                @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>

        <div class="auth-footer">
          <span>还没有账号？</span>
          <el-link type="primary" @click="$router.push('/reg')">立即注册</el-link>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import axios from "axios";
import { ElMessage } from "element-plus";
import router from "@/router";

// 定义对象来保存用户输入的登录数据
const user = ref({
  username: '',
  password: ''
});

// 定义登录方法
const login = () => {
  axios.post(BASE_URL + '/api/v1/users/public/login', user.value)
      .then((response) => {
        if (response.data.code == 2000) {
          ElMessage.success('登录成功');

          // // 存储用户信息
          switch (response.data.data.role) {
            case 1: localStorage.setItem('user', JSON.stringify(response.data.data)); break;
            case 2: localStorage.setItem('merchant', JSON.stringify(response.data.data)); break;
            case 3: localStorage.setItem('admin', JSON.stringify(response.data.data)); break;
          }


          // 根据角色跳转
          const roleRoutes = {
            1: 'user-home',
            2: 'merchant-home',
            3: 'admin-home'
          };
          const routeName = roleRoutes[response.data.data.role];
          console.log("尝试跳转到:", routeName);

          router.push({ name: routeName })
              .then(() => console.log("跳转成功"))
              .catch(err => console.error("跳转失败:", err));
        } else {
          ElMessage.error(response.data.msg);
          user.value = { username: '', password: '' };
        }
      })
      .catch((error) => {
        ElMessage.error('登录请求失败');
        console.error('登录错误:', error);
      });
}
</script>

<style scoped>
.auth-container {
  /* 原有样式保留 */
  height: 100vh;
  /*background-color: #fff;*/
  overflow: hidden;
  display: flex;
  flex-direction: column;

  /* 新增背景样式 */
  /*background-image: url('/img/login.png');*/
  background-image: url('../../public/imgs/login.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  position: relative;
}

.auth-box {
  /* 添加z-index让登录框浮在背景上层 */
  position: relative;
  z-index: 1;
  /* 原有样式保留 */
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
  padding: 0 30px;
}

.auth-header {
  padding: 30px;
  border-bottom: 1px solid #f0f0f0;
}

.logo-container {
  display: flex;
  align-items: center;
  gap: 10px;
}

.system-name {
  color: #333;
  font-size: 24px;
  font-weight: bold;
}

.auth-box {
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
  padding: 0 30px;
}

.auth-card {
  width: 450px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
  border: none;
}

.auth-title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 20px;
}

.auth-form {
  padding: 0 30px;
}

.auth-button {
  width: 100%;
  margin-top: 10px;
  height: 40px;
}

.auth-footer {
  text-align: center;
  color: #666;
  font-size: 14px;
  padding: 20px 0 0;
}

.auth-footer span {
  margin-right: 8px;
}
</style>